﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using ZMHWcfServiceLibrary;

namespace ZMHConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            /***********************************************************
            // Step 1 Create a URI to serve as the base address.
            Uri baseAddress = new Uri(
                //"http://localhost:8744/ZMHWcfServiceLibrary/"
                //"http://localhost:8744/ZMHWcfServiceLibrary/Calculator/"
                "http://localhost:8755/ZMHWcfServiceLibrary/"
                );
            // Step 2 Create a ServiceHost instance
            //ServiceHost selfHost = new ServiceHost(typeof(Calculator), baseAddress);
            ServiceHost selfHost = new ServiceHost(typeof(WCFWithDataContractSVC), baseAddress);
            try
            {
                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IWCFWithDataContractSVC), new BasicHttpBinding(), "WCFWithDataContractSVC");

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true
                };
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
            /***********************************************************
            Uri baseAddress = new Uri(
                "http://localhost:8799/Design_Time_Addresses/ZMHWcfServiceLibrary/MySingleton/"
              );
            MySingleton singleton = new MySingleton
            {
                Counter = 42
            };
            ServiceHost selfHost = new ServiceHost(singleton, baseAddress);
            try
            {    
                selfHost.AddServiceEndpoint(typeof(IMySingleton), new BasicHttpBinding(), "MySingleton");
                 
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true
                };
                selfHost.Description.Behaviors.Add(smb);
                selfHost.Open();
                Console.WriteLine("Service Start!");
                Console.ReadLine();
                selfHost.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception occurred: {0}", ex.Message);
                selfHost.Abort();
                Console.ReadLine();
            }
            /***********************************************************/
            Uri baseAddress = new Uri("http://localhost:8833/Design_Time_Addresses/ZMHWcfServiceLibrary/FaultExceptionSVC/"); 
            ServiceHost selfHost = new ServiceHost(typeof(FaultExceptionSVC), baseAddress);
            try
            {
                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IFaultExceptionSVC), new BasicHttpBinding(), "FaultExceptionSVC");

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true
                };
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
            /***********************************************************/
        }
    }
}
